package io.renren.modules.substation.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.substation.entity.TPointInfoDataConfEntity;
import io.renren.modules.substation.service.TPointInfoDataConfService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 遥信遥测遥控遥调点表-接收和配置
 *
 * @author yzx
 * @email sunlightcs@gmail.com
 * @date 2020-07-22 12:47:40
 */
@RestController
@RequestMapping("substation/tpointinfodataconf")
public class TPointInfoDataConfController extends AbstractController {
    @Autowired
    private TPointInfoDataConfService tPointInfoDataConfService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("substation:tpointinfodataconf:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tPointInfoDataConfService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("substation:tpointinfodataconf:info")
    public R info(@PathVariable("id") Long id){
		TPointInfoDataConfEntity tPointInfoDataConf = tPointInfoDataConfService.getById(id);

        return R.ok().put("tPointInfoDataConf", tPointInfoDataConf);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("substation:tpointinfodataconf:save")
    public R save(@RequestBody TPointInfoDataConfEntity tPointInfoDataConf){

        SysUserEntity sysUserEntity = getUser();
        //String dateStr = DateUtil.format(new Date(),"yyyyMMddHHmmss");

        if(tPointInfoDataConf.getCoefficient() == null || tPointInfoDataConf.getCoefficient().toString().equals("")){
            tPointInfoDataConf.setCoefficient(1);
        }
        if(tPointInfoDataConf.getOffsets() == null || tPointInfoDataConf.getOffsets().toString().equals("")){
            tPointInfoDataConf.setOffsets(0);
        }
        if(tPointInfoDataConf.getIsCad() == null || tPointInfoDataConf.getIsCad().toString().equals("")){
            tPointInfoDataConf.setIsCad("0");
        }
        tPointInfoDataConf.setCreateOpr(sysUserEntity.getUserId());
        tPointInfoDataConf.setCreateTime(new Date());
        tPointInfoDataConf.setLastOpr(sysUserEntity.getUserId());
        tPointInfoDataConf.setLastTime(new Date());

		tPointInfoDataConfService.save(tPointInfoDataConf);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("substation:tpointinfodataconf:update")
    public R update(@RequestBody TPointInfoDataConfEntity tPointInfoDataConf){

        SysUserEntity sysUserEntity = getUser();

        if(tPointInfoDataConf.getCoefficient() == null || tPointInfoDataConf.getCoefficient().toString().equals("")){
            tPointInfoDataConf.setCoefficient(1);
        }
        if(tPointInfoDataConf.getOffsets() == null || tPointInfoDataConf.getOffsets().toString().equals("")){
            tPointInfoDataConf.setOffsets(0);
        }
        if(tPointInfoDataConf.getIsCad() == null || tPointInfoDataConf.getIsCad().toString().equals("")){
            tPointInfoDataConf.setIsCad("0");
        }
        tPointInfoDataConf.setLastOpr(sysUserEntity.getUserId());
        tPointInfoDataConf.setLastTime(new Date());

		tPointInfoDataConfService.updateById(tPointInfoDataConf);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("substation:tpointinfodataconf:delete")
    public R delete(@RequestBody Long[] ids){
		tPointInfoDataConfService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
